package main.java.com.stylinlin.nio;

import java.io.*;
import java.util.Arrays;

/**
 * IOTest
 * 1、文件（file）：FileInputStream、FileOutputStream、FileReader、FileWriter
 * 2、数组（[]）：
 * 2.1、字节数组（byte[]）：ByteArrayInputStream、ByteArrayOutputStream
 * 2.2、字符数组（char[]）：CharArrayReader、CharArrayWriter
 * 3、管道操作：PipedInputStream、PipedOutputStream、PipedReader、PipedWriter
 * 4、基本数据类型：DataInputStream、DataOutputStream
 * 5、缓冲操作：BufferedInputStream、BufferedOutputStream、BufferedReader、BufferedWriter
 * 6、打印：PrintStream、PrintWriter
 * 7、对象序列化反序列化：ObjectInputStream、ObjectOutputStream
 * 8、转换：InputStreamReader、OutputStreWriter
 *
 * @author jonlin
 * @date 2020/7/2
 */
public class IOTest {
    public static void main(String[] args) {
        // 字节流
        String filePath = IOTest.class.getResource("/").getFile() + "main/resource/aa.txt";
        System.out.println("文件路径：" + filePath);
        try (
                InputStream in = new BufferedInputStream(new FileInputStream(filePath));
        ) {
            byte[] buff = new byte[1024];
            int bytesRead;
            while ((bytesRead = in.read(buff)) != -1) {
                System.out.println(new String(buff));
            }
        } catch (FileNotFoundException e) {
            System.out.println("未找到对应文件：" + filePath);
        } catch (IOException e) {
            System.out.println("文件处理异常");
        }

        // 字符流
        char[] buff = new char[1024];
        try (
                Reader buffReader = new BufferedReader(new FileReader(filePath));
        ) {
            while (buffReader.read(buff) != -1) {
                System.out.println(String.valueOf(buff));
            }
        } catch (FileNotFoundException e) {
            System.out.println("未找到对应文件：" + filePath);
        } catch (IOException e) {
            System.out.println("文件处理异常");
        }
    }
}
